package UDPTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import javax.swing.JFrame;

public class ClientUDP
{

	String name;
	String senha;
	static DatagramSocket clientSocket;
	public ClientUDP() throws SocketException{
		clientSocket = new DatagramSocket();
	}

	public static void main(String args[]) throws Exception
	{
		BufferedReader inFromUser =
				new BufferedReader(new InputStreamReader(System.in));
		ClientUDP c = new ClientUDP();
		int i = 0;
		do{
			i++;
			InetAddress IPAddress = InetAddress.getByName("localhost"); //conectar em 25.201.206.228 pelo notebook
			byte[] sendData = new byte[1024];
			byte[] receiveData = new byte[1024];

			System.out.println("Digite uma mensagem");

			String sentence = inFromUser.readLine();


			//      sendData = sentence.getBytes();
			//      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9879);
			//      clientSocket.send(sendPacket);

			//DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
			//clientSocket.receive(receivePacket);
			//String modifiedSentence = new String(receivePacket.getData());
			//System.out.println("CARALHOOO VAI " + modifiedSentence);
			TesteEnviarString(sentence);

			String s = testeReceber(receiveData);
			System.out.println("Aqui eu sei que funfa " + s);
		} while(i < 3);
		c.clientSocket.close();
	}


	public static String testeReceber(byte[] receiveData) throws IOException{
		DatagramPacket r = new DatagramPacket(receiveData, receiveData.length);
		clientSocket.receive(r);
		return (new String(r.getData()));
	}

	public static void TesteEnviarString (String message) throws IOException{
		byte[] x = message.getBytes();
		InetAddress IPAddress = InetAddress.getByName("localhost");
		DatagramPacket sendPacket = new DatagramPacket(x, x.length, IPAddress, 9879);
		clientSocket.send(sendPacket);

	}

}